using System;

namespace Wra4Skype.Library
{
    public static class MessageTools
    {
        private const string InvitationPrefix = "Wra4SKYPE";

        private const string CommandNameInvite = "INVITE";

        // this is for future use. It will mark exchanged messages so that in future
        // we can handle backward compatibility
        private const string Version = "2";

        /// <summary>
        /// Parses the given WRA invitation information into a message to be send through
        /// skype to the receipient. Uses latest version number.
        /// </summary>
        public static string CreateMessageFromInvitationParts(string password, string invitationXml)
        {
            return CreateMessageFromInvitationParts(password, invitationXml, Version);
        }

        /// <summary>
        /// Parses the given WRA invitation information into a message to be send through
        /// skype to the receipient
        /// </summary>
        public static string CreateMessageFromInvitationParts(string password, string invitationXml, string version)
        {
            if (version == "2")
            {
                invitationXml = StringTools.Compress(invitationXml);
            }

            return "{0}##{1}##{2}##{3}##{4}".Format2(InvitationPrefix, CommandNameInvite, version, password, invitationXml);
        }

        /// <summary>
        /// Unparses the given message, as received by the sender, and returns
        /// a instance of <see cref="InvitationMessage"/> with the invitation information.
        /// If message is not a WRA invitation returns null.
        /// </summary>
        public static InvitationMessage GetInvitationPartsFromMessage(string message)
        {
            // if message is invitation for remote assistance
            if (!message.StartsWith(InvitationPrefix + "##" + CommandNameInvite, StringComparison.Ordinal))
            {
                return null;
            }

            // after invitation prefix the password follows
            var invitationParts = message.Split("##".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (invitationParts.Length < 5)
            {
                throw new InvalidOperationException("Invitation does not have the expected content");
            }

            // take message parts
            var invitationMessage = new InvitationMessage
                                        {
                                            Version = invitationParts[2],                               // version of addin that send the message
                                            Password = invitationParts[3],                              // Password for the invitation
                                            InvitationXml = invitationParts[invitationParts.Length - 1] // invitation xml is last argument of invitation
                                        };

            // in version 2 it is compressed invitation xml is compressed
            if (invitationMessage.Version == "2")
            {
                invitationMessage.InvitationXml = StringTools.Decompress(invitationMessage.InvitationXml);
            }

            return invitationMessage;
        }
    }
}